package com.koron.standard.instructionStock.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.instructionStock.bean.po.Instruction;
import com.koron.standard.instructionStock.bean.query.InstructionQuery;
import com.koron.standard.instructionStock.bean.vo.InstructionVO;
import com.koron.standard.instructionStock.mapper.InstructionMapper;
import com.koron.standard.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 作业指导书库服务类
 * @author jzp
 * @since 2021/9/6
 */
@Service
public class InstructionService extends ServiceImpl<InstructionMapper, Instruction> {

    private Integer getMaxSort() {
        Integer maxSort = getBaseMapper().getMaxSort();
        return maxSort == null ? 0 : maxSort;
    }

    public InstructionVO getRealById(String id) {
        return getBaseMapper().getVoById(id);
    }

    public Page<InstructionVO> page(PageQuery<InstructionVO> pageQuery, InstructionQuery query) {
        // 设置默认只查有权限的水司数据
        query.setAppids(CommonUtils.getDefaultAppids(query.getAppids()));
        query.setPublicStatus(CommonUtils.getDefaultPublicStatus(query.getPublicStatus()));

        QueryWrapper<InstructionVO> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(query.getName()), "c_instruction.name", query.getName());
        wrapper.eq(StringUtils.isNotBlank(query.getTypeId()), "c_instruction.type_id", query.getTypeId());
        wrapper.in(CollUtil.isNotEmpty(query.getTypeIds()), "c_instruction.type_id", query.getTypeIds());
        wrapper.like(StrUtil.isNotEmpty(query.getTypeName()), "c_type.name", query.getTypeName());
        wrapper.eq(StringUtils.isNotBlank(query.getGroup()), "c_instruction.group", query.getGroup());

        if (query.getPublicStatus() != null && query.getPublicStatus() > 0) {
            // 如果是共有的
            wrapper.and(w -> w.eq("c_instruction.public_status", query.getPublicStatus())
                    .or(CollUtil.isNotEmpty(query.getAppids()))
                    .in(CollUtil.isNotEmpty(query.getAppids()), "c_instruction.appid", query.getAppids()));
        } else {
            wrapper.in(CollUtil.isNotEmpty(query.getAppids()), "c_instruction.appid", query.getAppids());
        }
        if (CollUtil.isNotEmpty(query.getDates())) {
            wrapper.between("c_instruction.create_time", query.getDates().get(0), query.getDates().get(1));
        }
        wrapper.eq("c_instruction.delete_flag", 0);

        Page<InstructionVO> page = pageQuery.convertPage();
        List<OrderItem> orders = pageQuery.getOrders();
        for (OrderItem order : orders) {
            if (order.getColumn().equals("type_name")) {
                order.setColumn("c_type.name");
            } else if (order.getColumn().equals("app_name")) {
                order.setColumn("s_app.name");
            } else {
                order.setColumn("c_instruction." + order.getColumn());
            }
        }
        getBaseMapper().selectPageVo(page, wrapper);
        return page;
    }

    @Override
    public boolean save(Instruction instruction) {

        instruction.setSort(getMaxSort()+1);
        // 1. 保存数据
        return super.save(instruction);

    }

    public void updateToPublic(List<String> ids) {
        List<Instruction> instructions = new ArrayList<>();
        for (String id : ids) {
            Instruction instruction = new Instruction();
            instruction.setId(id);
            instruction.setPublicStatus(1);
            instructions.add(instruction);
        }
        updateBatchById(instructions);
    }
}
